using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataTransferObject;

namespace DataAccess
{
   public class clsBoPhanDAO:clsDataAccessHelper 
    {
        // Fields
        private String tenTable;

        // Contructors
        public clsBoPhanDAO()
        {
            this.tenTable = "BOPHAN";
        }
        #region Methods
        //-------------------------------------Lấy dữ liệu bảng Bộ Phận-----------------------

       public clsBoPhanDTO TimBoPhanCuaNhanVien(String TenDangNhap)
       {
           String sql;
           DataTable dt;
           try
           {
               sql = "SELECT B.MABP,TENBP FROM " + this.tenTable;
               sql += " B, NHANVIEN N";
               sql += " WHERE TenDangNhap='" + TenDangNhap + "'";
               sql += " AND B.MABP=N.MABP";
               dt = new DataTable();
               dt = this.ExecuteQuery(sql);

               if (dt.Rows.Count > 0)
               {
                   clsBoPhanDTO BoPhanDTO = new clsBoPhanDTO();
                   BoPhanDTO.MaBP = dt.Rows[0].ItemArray[0].ToString();
                   BoPhanDTO.TenBP = dt.Rows[0].ItemArray[1].ToString();
                   return BoPhanDTO;
               }

               return null;
           }
           catch
           {
               return null;
           }
       }

        public DataTable SelectAll()
        {
            String sql;
            DataTable dt;
            try
            {
                sql = "SELECT * FROM " + this.tenTable;
                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       //-----------------------------------------------------------------------------//


        //-------------------------------------Thêm vào bảng Bộ Phận-----------------------

       public bool Insert(clsBoPhanDTO  bophan)
       {
           String sql;
           bool check = false;

           try
           {
               sql = "INSERT INTO " + this.tenTable;
               sql += " values('";
               sql += bophan.MaBP + "',N'";
               sql += bophan .TenBP + "')";
               check = this.ExecuteNonQuery(sql);

               return check;
           }
           catch
           {
               return check;
           }
       }

        //-----------------------------------------------------------------------------//



       //-------------------------------------Xóa vào bảng Bộ Phận-----------------------
       public bool Delete(clsBoPhanDTO bophan)
       {
           String sql;
           bool check = false;

           try
           {
               sql = "DELETE ";
               sql += " FROM " + this.tenTable;
               sql += " WHERE MABP LIKE '" + bophan.MaBP + "'";
               check = this.ExecuteNonQuery(sql);

               return check;
           }
           catch
           {
               return check;
           }
       }
       
    

       //-----------------------------------------------------------------------------//



       //-------------------------------------Sửa vào bảng Bộ Phận-----------------------

       public bool Update(clsBoPhanDTO bophan)
       {
           String sql;
           bool check = false;

           try
           {
               sql = "UPDATE " + this.tenTable;
               sql += " SET";
               sql += " TENBP = N'";
               sql += bophan .TenBP + "'";
               sql += " WHERE MABP LIKE '";
               sql += bophan .MaBP + "'";
               check = this.ExecuteNonQuery(sql);

               return check;
           }
           catch
           {
               return check;
           }
       }


       //-----------------------------------------------------------------------------//


        #endregion
    }
}
